 <?php
class Member extends CActiveRecord {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return "ts_member";
    }
    
    public function attributeLabels() {
        return array(
            "member_group_id" => "กลุ่มผู้ใช้งาน",
            "member_sex" => "เพศ",
            "member_username" => "Username",
            "member_password" => "Password",
            "member_firstname" => "ชื่อ",
            "member_lastname" => "นามสกุล",
            "member_birth_date" => "วันเกิด",
            "member_address" => "ที่อยู่",
            "member_email" => "Email",
            "member_tel" => "เบอร์โทร",
            "member_level" => "ระดับ",
            "member_status" => "สถานะ",
            "member_create_date" => "วันที่",
            "member_modified_date" => "วันที่แก้ไข",
        );
    }
    
    public function rules() {
        return array(
            array("member_username, member_password,member_firstname,member_lastname,member_birth_date,member_tel", "required",'message'=>'กรุณากรอกข้อมูลให้ครบ'),
            array("member_username", "length", "max" => 255),
            array("member_password", "length", "max" => 255),
            array("member_firstname", "length", "max" => 255),
            array("member_lastname", "length", "max" => 255),
            array("member_birth_date", "length", "max" => 100),
            array("member_tel", "length", "max" => 10),
            array('member_tel', 'numerical', 'integerOnly'=>true),
            array('member_email','email','message'=>'email not ok'),
            array('member_sex','in','range'=>array('f','m')) 
        );
    }
    
    public function search() {
        $criteria = new CDbCriteria;
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    public function beforeValidate() {
        if ($this->isNewRecord) {
            $this->member_status = 1;
            $this->member_group_id = 1;
            $this->member_create_date = new CDbExpression("NOW()");
            $this->member_level = "user";
        }
        else
        {
            $this->member_modified_date = new CDbExpression("NOW()");
        }

        return parent::beforeValidate();
    }
    
    public static function getSexName($sex) {
        if (!empty($sex)) {
            $arr = array(
                "f" => "หญิง",
                "m" => "ขาย"
            );
            return $arr[$sex];
        }
        return "";
    }
    
    
}
?>
